#!/usr/bin/env python3
"""
VSFTPD MCP Server

面向软件：vsftpd (Very Secure FTP Daemon)
所有 MCP tool 列表：

1. vsftpd_service_control
   - 用途：管理 vsftpd 服务的启动、停止、重启、重载等操作
   - 参数：action (str) - 操作类型：start|stop|restart|reload|status
   - 返回结构：统一 JSON 格式

2. vsftpd_target_control
   - 用途：管理 vsftpd.target 的启动、停止、状态查询等操作
   - 参数：action (str) - 操作类型：start|stop|status
   - 返回结构：统一 JSON 格式
"""

import subprocess
import json
from typing import Dict, Any
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("VSFTPD MCP Server")

@mcp.tool()
def vsftpd_service_control(action: str) -> Dict[str, Any]:
    """
    管理 vsftpd 服务的系统控制操作
    
    参数:
        action: 操作类型，支持 start|stop|restart|reload|status
    
    返回:
        JSON 结构:
        {
            "success": bool,      # 操作是否成功
            "command": str,       # 执行的完整命令
            "exit_code": int,     # 命令退出码
            "stdout": str,        # 标准输出
            "stderr": str         # 标准错误输出
        }
    """
    valid_actions = ["start", "stop", "restart", "reload", "status"]
    if action not in valid_actions:
        return {
            "success": False,
            "command": f"systemctl {action} vsftpd.service",
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Invalid action: {action}. Valid actions are: {', '.join(valid_actions)}"
        }
    
    command = ["systemctl", action, "vsftpd.service"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command execution timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Command execution failed: {str(e)}"
        }

@mcp.tool()
def vsftpd_target_control(action: str) -> Dict[str, Any]:
    """
    管理 vsftpd.target 的系统控制操作
    
    参数:
        action: 操作类型，支持 start|stop|status
    
    返回:
        JSON 结构:
        {
            "success": bool,      # 操作是否成功
            "command": str,       # 执行的完整命令
            "exit_code": int,     # 命令退出码
            "stdout": str,        # 标准输出
            "stderr": str         # 标准错误输出
        }
    """
    valid_actions = ["start", "stop", "status"]
    if action not in valid_actions:
        return {
            "success": False,
            "command": f"systemctl {action} vsftpd.target",
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Invalid action: {action}. Valid actions are: {', '.join(valid_actions)}"
        }
    
    command = ["systemctl", action, "vsftpd.target"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command execution timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Command execution failed: {str(e)}"
        }

if __name__ == "__main__":
    mcp.run()